<!DOCTYPE html>
<html lang="cn">
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta charset="utf-8" />
		<title>创建模板</title>
		<meta name="description" content="overview &amp; stats" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
	</head>
    <style>
        input{
            border: 1px solid #D5D5D5;
        }

        .custom-template{
            border: 1px dashed #ccc !important;
        }
    </style>
    <body>
        <div class="col-xs-12 no-padding">
            <!-- PAGE CONTENT BEGINS -->
            <h4 class="lighter">
                <div class="" style="" >
                    <input class="" type="text" placeholder="scrname" id="scrname" style="height: 34px;">
                    <button id="buildHtml" class="btn btn-sm btn-info" type="button">
                        生成模板
                    </button>
                    

                    <button id="loadContent" class="btn btn-sm btn-info" type="button">
                        预览
                    </button>
                    <button id="windowLoadContent" class="btn btn-sm btn-info" type="button">
                        窗口预览
                    </button>
                </div>
                
            </h4>

            <div class="hr hr-18 hr-double dotted"></div>

            <div class="widget-box">
                <div class="widget-header widget-header-blue widget-header-flat">
                    <h4 class="widget-title lighter">预览</h4>

                    <div class="widget-toolbar">
                        <label style="vertical-align: baseline;">
                            <button id="downloadHtml" class="btn btn-sm btn-info" type="button" style="padding: 1px 4px;">
                                下载模板
                            </button>
                        </label>
                    </div>
                </div>

                <div class="widget-body">
                    <div id="showDefault" class="widget-main" style="padding:0px !important;position: relative;margin: 5px;">

                    </div><!-- /.widget-main -->
                </div><!-- /.widget-body -->
            </div>

        </div>
        <script type="text/javascript"> 
            var ins_argjson={};
            $(".page-content-area").ace_ajax("loadScripts", new Array(), function() {
                $("#showDefault").height($(window).height()-290)

                $.custom().then(({custom,C_DataSource,C_Where,C_Grid})=>{
                    ins_argjson.custom = custom;
                    ins_argjson.C_DataSource = C_DataSource;
                    ins_argjson.C_Where = C_Where;
                    ins_argjson.C_Grid = C_Grid;

                    //初始化
                    init();

                    //绑定事件
                    bindClick();

                })
                
            });

            //初始化
            function init(){

            }

            //绑定事件
            function bindClick(){
                //生成模板
                $("#buildHtml").unbind("click").click(function(){
                    $("#showDefault").empty()
                    let scrname = $("#scrname").val()
                    let params = {"jn":JSON.stringify({"arg":{"scrname":scrname}})}
                    //初始化custom
                    initCustom(params)
                })

                //预览
                $("#loadContent").unbind("click").click(function(){
                    //加载form，grid等组件
                    loadContent()
                })

                //窗口预览
                $("#windowLoadContent").unbind("click").click(function(){
                    let scrname = $("#scrname").val()
                    var s_width = $(window).width()*0.9
                    var s_height = $(window).height()*0.9
                    window.open("#/html/custom/custom_template_view.html?&custom="+scrname,"_blank",{width:parseInt(s_width),height:parseInt(s_height),center:'yes'})
                })

                

                //下载HTML
                $("#downloadHtml").unbind("click").click(function(){
                    let dom = document.getElementById('showDefault');
                    let data = dom.innerHTML;
                    let scrname = $("#scrname").val()
                    let name = 'custom_'+scrname+'.html';
                    let customHtml = customHtmlBuild(data,scrname);

                    electron.ipcRenderer.send("asynchronous-message",customHtml ,name);

                    electron.ipcRenderer.on("asynchronous-reply", function(event, arg ,arg1) {
                        // 这里的arg是从主线程请求的数据
                        console.log("render+" + arg); 
                        // debugger
                        // window.open("#/html/custom/"+arg1,"_self")
                    });
                })
            }

            //初始化custom
            function initCustom(params){
                ins_argjson.C_DataSource.ScreenBizData = ins_argjson.C_DataSource.getScreenBizDefault(params)
                let ScreenData = ins_argjson.C_DataSource.ScreenBizData
                var param = {}
                param.boxId = "#showDefault"
                param.onlyInitTemplate = "yes"          //仅加载模板
                ins_argjson.custom.init(ScreenData,param,function(){
                    
                })
            }

            //加载form，grid等组件
            function loadContent(){
                ins_argjson.custom.initComponent()
            }
        </script>
	</body>
</html>